package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageDetailSNActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageSaleOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleOverviewBean;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class SaleOutStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<ItemListBean> mSaleOutStorageItemList;
    private LayoutInflater inflater;
    private OutStorageSaleOverviewActivity.OutStorageSaleOverviewListener mOutStorageSaleListener;
    private String mMaterielCode, mCustomer, mContact, mContactPhone, mOrderType, mShippingType, mCreateTime, mAddress;

    public SaleOutStorageItemListAdapter(Context context, OutStorageSaleOverviewBean.BodyBean data, List<ItemListBean> outStorageSaleItemList, String materielCode, OutStorageSaleOverviewActivity.OutStorageSaleOverviewListener outStorageSaleOverviewListener) {
        mContext = context;
        mCustomer = data.getReceive_name();
        mContact = data.getContact_person();
        mContactPhone = data.getContact_phone();
        mOrderType = data.getOrder_type_name();
        mShippingType = data.getShipping_type();
        mCreateTime = data.getShipping_time();
        mAddress = data.getAddress();

        mSaleOutStorageItemList = outStorageSaleItemList;
        mOutStorageSaleListener = outStorageSaleOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mSaleOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mSaleOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_sale_item_list_first_item, null);
            TextView customer = (TextView) convertView.findViewById(R.id.customer);
            TextView contact = (TextView) convertView.findViewById(R.id.contact);
            TextView contactPhone = (TextView) convertView.findViewById(R.id.contactPhone);
            TextView orderType = (TextView) convertView.findViewById(R.id.orderType);
            TextView shippingType = (TextView) convertView.findViewById(R.id.shippingType);
            TextView createTime = (TextView) convertView.findViewById(R.id.createTime);
            TextView address = (TextView) convertView.findViewById(R.id.address);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            customer.setText(mCustomer);
            contact.setText(mContact);
            contactPhone.setText(mContactPhone);
            orderType.setText(mOrderType);
            shippingType.setText(mShippingType);
            createTime.setText(mCreateTime);
            address.setText(mAddress);
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_out_storage_sale_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_out_storage_sale_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mOutStorageSaleListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageSaleListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            ItemListBean saleOutstorageItem = mSaleOutStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, saleOutstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.rid = (TextView) view.findViewById(R.id.rid);
        viewHolder.type = (TextView) view.findViewById(R.id.type);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ItemListBean saleOutStorageItem) {
        viewHolder.rid.setText(saleOutStorageItem.getRefer_receipt_rid());
        viewHolder.type.setText(saleOutStorageItem.getMat_store_type());
        viewHolder.materielCode.setText(saleOutStorageItem.getMat_code());
        viewHolder.materielDescription.setText(saleOutStorageItem.getMat_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(saleOutStorageItem.getActualSendNumber()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + saleOutStorageItem.getActualSendNumber() + saleOutStorageItem.getUnit_name()));
        if (TextUtils.isEmpty(saleOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent;
                String rid;
                switch (saleOutStorageItem.getMat_store_type()) {
                    case OutStorageUtils.TYPE_BATCH:
                        //批次
                        break;
                    case OutStorageUtils.TYPE_SN:
                        //序列号
                        intent = new Intent(context, OutStorageDetailSNActivity.class);
                        intent.putExtra(OutStorageExtra.SERIAL, saleOutStorageItem.getRefer_receipt_rid());
                        intent.putExtra(OutStorageExtra.TYPE, OutStorageUtils.OUT_STORAGE_TYPE_SALE);
                        context.startActivity(intent);
                        break;
                    case OutStorageUtils.TYPE_NONE:
                        //无

                        break;
//                    default:
//                        //测试按序列号
//                        intent = new Intent(context, OutStorageDetailSNActivity.class);
//                        intent.putExtra(OutStorageExtra.SERIAL, saleOutStorageItem.getRefer_receipt_rid());
//                        intent.putExtra(OutStorageExtra.TYPE, OutStorageUtils.OUT_STORAGE_TYPE_SALE);
//                        context.startActivity(intent);
//                        break;

                }
            }
        });
//        viewHolder.serialNumber.setText(saleOutStorageItem.getStock_output_rid());
//        viewHolder.materielCode.setText(saleOutStorageItem.getMat_code());
//        viewHolder.materielDescribe.setText(saleOutStorageItem.getMat_name());
////        String canSendNumber = ArithUtils.sub(saleOutStorageItem.getStock_qty(), saleOutStorageItem.getSend_qty()).toString();
//        viewHolder.canSendNumber.setText(mContext.getResources().getString(R.string.activity_out_storage_allocating_item_list_can_send) + saleOutStorageItem.getLast_qty() + saleOutStorageItem.getUnit_name());
//        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(saleOutStorageItem.getActualSendNumber()) ? view.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number)
//                : (view.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + saleOutStorageItem.getActualSendNumber() + saleOutStorageItem.getUnit_name()));
//
//        if (TextUtils.isEmpty(saleOutStorageItem.getActualSendNumber())) {
//            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
//        } else {
//            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
//        }
//
//        view.setOnClickListener(new NoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View v) {
//                Intent intent = new Intent(context, OutStorageSaleDetailActivity.class);
//                String serial = viewHolder.serialNumber.getText().toString();
//                intent.putExtra(OutStorageExtra.SERIAL, serial);
//                context.startActivity(intent);
//            }
//        });
    }

    private class ViewHolder {
        private TextView materielCode, materielDescription, outStorageNumber, rid, type;

        private PercentRelativeLayout headLayout;
    }
}
